const Advertisement = require("../../model/advertisement.model");

class AdvertisementService {
  async getAdvertisement(args) {
    const { current = 1, pageSize = 10, ...remainingParameters } = args;
    const offset = (current - 1) * pageSize;
    const { rows: list, count: total } = await Advertisement.findAndCountAll({
      limit: Number(pageSize),
      offset: offset,
      where: {
        ...remainingParameters,
      },
      order: [["type"],["sort"]],
    });
    return {
      current,
      pageSize,
      total,
      list,
    };
  }
  async addAdvertisement(args) {
    const { id, createdAt, is_it_enabled, updatedAt, ...remainingParameters } =
      args;
    const res = await Advertisement.create(remainingParameters);
    return res;
  }
  async updateAdvertisement(args) {
    const { id, createdAt, updatedAt, ...remainingParameters } = args;
    const resource = await Advertisement.update(
      {
        ...remainingParameters,
      },
      {
        where: {
          id,
        },
      }
    );
    return resource;
  }
  async deleteAdvertisement(args) {
    const { id } = args;
    const res = await Advertisement.destroy({
      where: {
        id,
      },
    });
    return res;
  }
}

module.exports = new AdvertisementService();
